#!/bin/bash

# JDFS2 Build Script
# 编译JDFS2二进制文件

set -e  # 遇到错误立即退出

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Go环境
check_go() {
    if ! command -v go &> /dev/null; then
        print_error "Go is not installed or not in PATH"
        exit 1
    fi
    
    GO_VERSION=$(go version | awk '{print $3}')
    print_info "Using Go version: $GO_VERSION"
}

# 清理旧的构建文件
cleanup() {
    print_info "Cleaning up old build artifacts..."
    rm -f jdfs2
    rm -f coverage.out coverage.html
    print_success "Cleanup completed"
}

# 构建二进制文件
build() {
    print_info "Building JDFS2 binary..."
    
    # 设置构建参数
    BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
    GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
    VERSION="1.0.0"
    
    # 构建标志
    LDFLAGS="-X main.Version=$VERSION"
    LDFLAGS="$LDFLAGS -X main.BuildTime=$BUILD_TIME"
    LDFLAGS="$LDFLAGS -X main.GitCommit=$GIT_COMMIT"
    
    # 执行构建
    if go build -ldflags "$LDFLAGS" -o jdfs2; then
        print_success "Build completed successfully"
        
        # 显示构建信息
        if [ -f "jdfs2" ]; then
            BINARY_SIZE=$(ls -lh jdfs2 | awk '{print $5}')
            print_info "Binary size: $BINARY_SIZE"
            print_info "Binary location: $(pwd)/jdfs2"
        fi
        
        # 测试二进制文件
        print_info "Testing binary..."
        if ./jdfs2 version > /dev/null 2>&1; then
            print_success "Binary test passed"
            ./jdfs2 version
        else
            print_error "Binary test failed"
            exit 1
        fi
    else
        print_error "Build failed"
        exit 1
    fi
}

# 显示帮助信息
show_help() {
    echo "JDFS2 Build Script"
    echo ""
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -h, --help     Show this help message"
    echo "  -c, --clean    Clean build artifacts before building"
    echo "  -v, --verbose  Verbose output"
    echo ""
    echo "Examples:"
    echo "  $0              # Build JDFS2"
    echo "  $0 -c           # Clean and build"
    echo "  $0 -v           # Verbose build"
}

# 主函数
main() {
    local CLEAN=false
    local VERBOSE=false
    
    # 解析命令行参数
    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help)
                show_help
                exit 0
                ;;
            -c|--clean)
                CLEAN=true
                shift
                ;;
            -v|--verbose)
                VERBOSE=true
                shift
                ;;
            *)
                print_error "Unknown option: $1"
                show_help
                exit 1
                ;;
        esac
    done
    
    # 设置详细输出
    if [ "$VERBOSE" = true ]; then
        set -x
    fi
    
    print_info "Starting JDFS2 build process..."
    
    # 检查Go环境
    check_go
    
    # 清理（如果需要）
    if [ "$CLEAN" = true ]; then
        cleanup
    fi
    
    # 构建
    build
    
    print_success "Build process completed successfully!"
    print_info "You can now run: ./jdfs2 start"
}

# 运行主函数
main "$@"
